package com.shgsh.server.client;

import com.shgsh.server.message.Message;
import com.shgsh.server.model.IMUser;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * Created by jessy on 2015/8/16.
 */
public class YQClient2 {
    private String id;
    public Socket s;
    public YQClient2(String id){
        this.id=id;
    }
    public boolean sendLoginInfo(Object obj){
        boolean b=false;
        try {
            s=new Socket();
            try{
                s.connect(new InetSocketAddress("127.0.0.1",8888),2000);
            }catch(SocketTimeoutException e){
                //连接服务器超时
                return false;
            }
            ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(obj);
            ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
            Message ms=(Message)ois.readObject();
            System.out.println("ms = " + ms.getMsg());
            //个人信息

            //创建一个该账号和服务器保持连接的线程
            ClientConServerThread2 ccst2=new ClientConServerThread2(((IMUser)ms.getData()).getId(),s);
            //启动该通信线程
            System.out.println("start client2 thread ...... ");
            ccst2.start();
            //加入到管理类中
            ManageClientConServer.addClientConServerThread(((IMUser)ms.getData()).getId(), ccst2);
            b=true;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return b;
    }
}
